package com.ly.mpayment.rest.up.xinjin;

import com.alibaba.fastjson.JSONObject;
import com.ly.mpayment.core.secuity.MD5;
import com.ly.mpayment.core.util.*;
import com.ly.mpayment.persist.entity.UpOrder;
import com.ly.mpayment.persist.entity.UpShopInfo;
import com.ly.mpayment.persist.service.downrechargeflow.IDownRechargeFlowService;
import com.ly.mpayment.persist.service.downshopinfo.IDownShopInfoService;
import com.ly.mpayment.persist.service.uporder.IUporderService;
import com.ly.mpayment.persist.service.upshopinfo.IUpShopInfoService;
import com.ly.mpayment.rest.recharge.RechargeRest;
import org.apache.log4j.Logger;
import org.mortbay.util.MultiMap;
import org.mortbay.util.UrlEncoded;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;

@Component
@Path("xinjin")
public class XinJinRest {

    Logger logger = Logger.getLogger(XinJinRest.class);

    @Autowired
    IUporderService upOrderService;

    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Autowired
    IDownRechargeFlowService flowService;

    @Autowired
    IDownShopInfoService shopService;

    @Autowired
    IUpShopInfoService upShopInfoService;

    public void queryOrder(UpOrder order, UpShopInfo shop) {
        JSONObject data = new JSONObject();
        data.put("userid",shop.getAppId());
        data.put("seqid",order.getUporderuuid());
        //MD5(userid+ seqId+ 秘钥)
        data.put("sign", MD5.md5(shop.getAppId()+ order.getUporderuuid() + shop.getUpkey()));
        logger.info("queryOrder_xinjin:"+shop.getOrderStatusAddress().concat("?").concat(StringSort.jsonToString(data)));
        String result = HttpClient.httpGetUrl(shop.getOrderStatusAddress().concat("?").concat(StringSort.jsonToString(data)));
        logger.info("resultxinjin:"+result);
        if(result != null){
            JSONObject json = JSONObject.parseObject(result);
            if(json.containsKey("state")){
                //订单状态 // 0失败1成功 2处理中
                if(json.getString("state").equals("3") ){
                    order.setRemark( json.containsKey("certificateNo") ? json.getString("certificateNo") : json.getString("desc"));
                    RechargeRest.successUpOrder.add(order);
                } else if (json.getString("state").equals("4")){
                    order.setRemark(json.getString("desc"));
                    RechargeRest.failtUpOrder.add(order);
                }
            }
        }else{
            //不处理 走查询 确认
            upOrderService.updateUpOrderMsg(order, "","查单失败:"+result);
            logger.info("查单失败"+order.getUpphonenum());
        }
    }
    public void commitOrder(UpOrder order, UpShopInfo shop) {
        JSONObject data = new JSONObject();
        data.put("userid",shop.getAppId());
        data.put("orderid",order.getUporderuuid());
        data.put("number",order.getUpphonenum());
        data.put("norms",order.getUpShopPro().getUpfacevalue());
        data.put("ot","2");
        data.put("ut",0);
        data.put("yt",order.getOperators().equals("2edb5b9c-9f99-11e7-9dc0-fcaa1456507e") ? "1" :
                order.getOperators().equals("2ee16927-9f99-11e7-9dc0-fcaa1456507e") ? "2" :
                        order.getOperators().equals("2ee70961-9f99-11e7-9dc0-fcaa1456507e") ? "3" : "6");
        //MD5(userid+ orderid+ number+ norms+ ot+ ut+ 秘钥)
        data.put("sign", MD5.md5( shop.getAppId()+order.getUporderuuid()+order.getUpphonenum()+order.getUpShopPro().getUpfacevalue()+
                data.getString("ot")+data.getString("ut")+shop.getUpkey() ));
        logger.info("commitOrder_xinjin:"+shop.getOrderCommitAddress().concat("?").concat(StringSort.jsonToString(data)));
        String result = HttpClient.httpGetUrl(shop.getOrderCommitAddress().concat("?").concat(StringSort.jsonToString(data)));
        logger.info("commitOrder_result:"+result);
        if(!StringUtils.isBlank(result)){
            JSONObject json = JSONObject.parseObject(result);
            if(json.getString("state").equals("0")){
                upOrderService.updateUpOrderMsg(order, json.getString("seqid"), json.getString("desc"));
            }else if(!json.getString("state").equals("0")){
                order.setRemark(json.getString("desc"));
                RechargeRest.failtUpOrder.add(order);
            }else{
                upOrderService.updateUpOrderMsg(order, "",json.getString("desc"));
            }
        }else{
            //不处理 走查询 确认
            upOrderService.updateUpOrderArtificial(order, "","提单失败:"+result);
            logger.info("提单失败"+order.getUpphonenum());
        }
    }


    @Path("callBack")
    @POST
    public String callBack(@RequestBody String body){
        logger.info("xinjincallBack:"+body);
        if(!StringUtils.isBlank(body)){
            JSONObject data = JSONObject.parseObject(body);
            UpOrder order = upOrderService.findById(data.getString("orderid"));
            if(order != null ){
                //订单状态 // 0失败1成功 2处理中
                if (order != null && data.getString("state").equals("3") && order.getStatus() == Config.status[0]) {
                    order.setRemark(data.containsKey("certificateNo") ? data.getString("certificateNo") : data.getString("desc"));
                    RechargeRest.successUpOrder.add(order);
                    return "1";
                }else if(order != null && order.getStatus() == Config.status[0] && data.getString("state").equals("4") ){
                    order.setRemark(data.getString("desc"));
                    RechargeRest.failtUpOrder.add(order);
                    return "1";
                }else{
                    logger.info("xinjincallBack_not_found_order:" + data );
                }
            }else {
                logger.info("未找到订单:"+data);
            }
        }
        return "1";
    }
}
